//pack11jsclass

var VisitObjectPopup = new visitObjectPopup();

function visitObjectPopup() {

    this.initialized=false;
    this.element=document.getElementById("PopupBody");
    this.form=document.getElementById("PopupBody");
    this.extra=document.getElementById("PopupBody");
    this.signed=false;

    this.setSign = function(element) {
        VisitObjectPopup.signed=element.checked;
    }
    
    this.open = function() {
        if(!this.initialized) this.init();
        if(!this.initialized) return;
        this.changedElements=new Array();
        this.element.style.display="block";
        try {
            document.getElementById("popupContainer").style.width=document.getElementById("VisitObjectPopup_width").value;
        } catch(e) {

        }
        PopupManager.container.style.display="block";
        PopupManager.currentPopup=this;
        PopupManager.popupActive=true;
    }

    this.init = function() {

        try {
            this.element=document.getElementById("VisitObjectPopup");
            this.form=document.getElementById("VisitObjectForm");
            this.extra=document.getElementById("VisitObjectForm_extra");
        } catch(e) {
            Trace("Could not find VisitObjectPopup");
            return;
        }

        buttons=this.element.getElementsByTagName("input");
        for(tpb=0; tpb<buttons.length; tpb++) {
            button=buttons[tpb];
            if(button.id=="VisitObjectSubmit") {
                button.onclick=this.submit;
            }

            if(button.id=="VisitObjectCancel") {
                button.onclick=function() {
                PopupManager.close();
                }
            }
        }

        this.initialized=true;
    }

    this.submit = function() {
        objectCookie=getCookie("dekaro"+document.getElementById("object_id").value);
        if(objectCookie || !VisitObjectPopup.signed) {
            //alert("Stop!")
            return false;
        }
        checker=0;
        alert_text=document.getElementById("VisitObjectForm_extra").value+"\n\n";
        if(document.getElementById("VisitorName_text").value.length>=2) {
            checker++;
        } else {
            ttt=document.getElementById("VisitorName_label").innerHTML.split("<");
            alert_text+=ttt[0]+"\n";
        }
        if(document.getElementById("VisitorPhone_text").value.length>=8) {
            checker++;
        } else {
            ttt=document.getElementById("VisitorPhone_label").innerHTML.split("<");
            alert_text+=ttt[0]+"\n";
        }
        if(document.getElementById("VisitorMail_text").value.length>=2 & document.getElementById("VisitorMail_text").value.split("@").length==2) {
            checker++;
        } else {
            ttt=document.getElementById("VisitorMail_label").innerHTML.split("<");
            alert_text+=ttt[0]+"\n";
        }

        if(checker==3) {

            addressVar=new ajaxVar();
            addressVar.name="address";
            addressVar.value=document.getElementById("VisitObjectPopup_address").innerHTML;
            AjaxManager.variables.push(addressVar.query());

            idVar=new ajaxVar();
            idVar.name="id";
            idVar.value=document.getElementById("ObjectToVisit").value;
            AjaxManager.variables.push(idVar.query());

            nameVar=new ajaxVar();
            nameVar.name="name";
            nameVar.value=document.getElementById("VisitorName_text").value;
            AjaxManager.variables.push(nameVar.query());

            dayVar=new ajaxVar();
            dayVar.name="day";
            dayVar.value=document.getElementById("VisitorDay_select").value;
            AjaxManager.variables.push(dayVar.query());

            phoneVar=new ajaxVar();
            phoneVar.name="phone";
            phoneVar.value=document.getElementById("VisitorPhone_text").value;
            AjaxManager.variables.push(phoneVar.query());

            mailVar=new ajaxVar();
            mailVar.name="mail";
            mailVar.value=document.getElementById("VisitorMail_text").value;
            AjaxManager.variables.push(mailVar.query());

            timeFromVar=new ajaxVar();
            timeFromVar.name="time_from";
            timeFromVar.value=document.getElementById("VisitorTime_from_select").value;
            AjaxManager.variables.push(timeFromVar.query());

            timeToVar=new ajaxVar();
            timeToVar.name="time_to";
            timeToVar.value=document.getElementById("VisitorTime_to_select").value;
            AjaxManager.variables.push(timeToVar.query());

            c_value=new ajaxVar();
            c_value.name="c_value";
            c_value.value=document.getElementById("c_value_text").value;
            AjaxManager.variables.push(c_value.query());

            c_id=new ajaxVar();
            c_id.name="cid";
            c_id.value=document.getElementById("cid").value;
            AjaxManager.variables.push(c_id.query());

        AjaxManager.url="?response=VisitObjectResponder";
        AjaxManager.backHandler=VisitObjectPopup.ajaxHandler;
        //AjaxManager.setLoader("VisitObjectPopupLoader");
        document.getElementById("VisitObjectPopupLoader").innerHTML=AjaxManager.loader;
        AjaxManager.callServer();
        } else {
            alert(alert_text);
        }
    }

    this.ajaxHandler = function(text,xml) {
        setCookie("dekaro"+document.getElementById("object_id").value,true,1);
        PopupManager.close();
        var al=text.split("::");
        if (al.length>1) {
            alert(al[0]);
            document.getElementById("ObjectCardVisitLink").innerHTML=al[1];
        } else {
            alert(text);
        }
        //alert(text+" "+objectCookie);
    }

    this.checkTime = function(element) {
        selTo=document.getElementById("VisitorTime_to_select");
        selFrom=document.getElementById("VisitorTime_from_select");
        ntTo=selTo.value.split(":");
        ntFrom=selFrom.value.split(":");
        valueTo=(ntTo.length>1)? ntTo[0]:selTo.value;
        valueFrom=(ntFrom.length>1)? ntFrom[0]:selFrom.value;
        if(element.id.indexOf("_to_")>=0) {
            timeDir="to";
        } else if(element.id.indexOf("_from_")>=0) {
            timeDir="from";
        } else {
            return false;
        }

        if(timeDir=="to") {
            if(Number(valueFrom)>=Number(valueTo)) {
                selFrom.selectedIndex=0;
            }
        } else {
            if(Number(valueFrom)>=Number(valueTo)) {
                selTo.selectedIndex=selTo.options.length-1;
            }
        }
    }
	
}